<?php

namespace Common\Model;

use Think\Model;

class CompanyModel extends Model {
    /**
     * 钱包变动
     * @param type $param
     */
    public function changeWallet($company_id=0,$change_type=0,$amount=0,$order_id=0) {
        $total_amount = $this->where(array('company_id'=>$company_id))->getField('total_amount');
        switch ($change_type) {
            case 1:
            case 2:
                $amount = -$amount;
                $add_subtract = 2;
                break;
            case 5:
            case 6:
                $amount = $amount;
                $add_subtract = 1;
                break;
        }
        D()->startTrans();
        $companyRes = $this->where(array('company_id'=>$company_id))->save(array('total_amount'=>['exp','total_amount+('.$amount.')']));
        $new_total_amount = $this->where(array('company_id'=>$company_id))->getField('total_amount');
        $data['company_id'] = $company_id;
        $data['change_type'] = $change_type;
        $data['add_subtract'] = $add_subtract;
        $data['change_price'] = $amount;
        $data['wallet_balance'] = $new_total_amount;
        $data['order_id'] = $order_id;
        $walletRes = M('wallet_change')->add($data);
        if($companyRes && $walletRes){
            D()->commit();
            return TRUE;
        }else{
            D()->rollback();
            return FALSE;
        }
        
    }
}
